package com.fhz.a2102shopjava.ui.dashboard.ui.right;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fhz.a2102shopjava.R;
import com.fhz.a2102shopjava.entity.Category;
import com.fhz.a2102shopjava.util.ImageLoader;

import java.util.List;

/**
 * 时间:2023/8/16
 *
 * @author Mr.Feng
 * 简述: 首页商品中的适配器
 */
public class DashboardRightRecyclerViewAdapter extends RecyclerView.Adapter<DashboardRightRecyclerViewAdapter.ViewHolder> {

    private Context mContext;
    private List<Category> dataList;

    public DashboardRightRecyclerViewAdapter(Context context, List<Category> dataList) {
        mContext = context;
        this.dataList = dataList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView categoryTitle;
        private ImageView mImageView;
        private OnItemClickListener mListener;// 声明自定义监听接口

        ViewHolder(@NonNull View itemView,OnItemClickListener listener) {
            super(itemView);
            mListener = listener;
            itemView.setOnClickListener(this); // 为ItemView添加点击事件
            categoryTitle = itemView.findViewById(R.id.right_category_title);
            mImageView = itemView.findViewById(R.id.right_image);
        }

        @Override
        public void onClick(View v) {
            mListener.onItemClick(v, getLayoutPosition());
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_fragment_right_rv_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view,mClickListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Category category = dataList.get(position);
        holder.categoryTitle.setText(category.getCategoryName());
        ImageLoader.load(holder.mImageView.getContext(), category.getCategoryIcon(), holder.mImageView);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    private OnItemClickListener mClickListener;//自定义的接口

    public interface OnItemClickListener {
        public void onItemClick(View view, int postion);
    }

    public void setItemClickListener(OnItemClickListener clickListener) {
        mClickListener = clickListener;
    }
}
